package com.demo2do.fms.admin.entity.search.authority;

import java.util.HashMap;
import java.util.Map;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

/**
 *
 * @author Ark
 *
 */
public class UserSearch implements QueryBuilder {

    /** 姓名 */
	private String name;
	
	/** 登录名 */
	private String loginName;
	
	/** 所属供应商 */
	private Long supplierId;
	
	/** 所属运输业务公司 */
	private Long orgId;
    
    private QueryFilter filter;
    
    private QueryOrder order;
    
    /**
     * The default constructor
     */
    public UserSearch() {
        order = new QueryOrder();
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
    	
        if (filter == null || refresh) {
        	
            filter = new QueryFilter().addLike("name", name)
            						  .addLike("loginName", loginName)
            						  .addEquals("supplierId", supplierId)
            						  .addEquals("orgId", orgId);
            		
        }
        
        return filter;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        String sentence = "FROM User Where disabled = false" + getPreparedFilter(false).clauseSentence() + order.sentence();
        return sentence;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }
    
    /**
     * @param authority
     */
    public void addAuthority(String authority) {
    	
    	this.getPreparedFilter(false)
    		.addSentence("authority LIKE :authority", "authority", authority);
    	
    }
    
    /**
     * @param authority1
     * @param authority2
     */
    public void addAuthority(String authority1, String authority2) {
    	
    	Map<String, Object> authorities = new HashMap<String, Object>();
    	authorities.put("authority1", authority1);
    	authorities.put("authority2", authority2);
    	
    	this.getPreparedFilter(false)
    		.addSentence(" (authority LIKE :authority1 OR authority LIKE :authority2) ", authorities);
    	
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        return order;
    }
    
    /**
     * @return
     */
    public QueryFilter getFilter(){
        return getPreparedFilter(false);
    }

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the loginName
	 */
	public String getLoginName() {
		return loginName;
	}

	/**
	 * @return the supplierId
	 */
	public Long getSupplierId() {
		return supplierId;
	}
	
	/**
	 * @return
	 */
	public Long getOrgId() {
        return orgId;
    }

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param loginName the loginName to set
	 */
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	/**
	 * @param supplierId the supplierId to set
	 */
	public void setSupplierId(Long supplierId) {
		this.supplierId = supplierId;
	}
	
	/**
	 * @param orgId
	 */
	public void setOrgId(Long orgId) {
        this.orgId = orgId;
    }

}